#pragma once

#include <iostream>
#include "Connection.hpp"
#include "Protocol.hpp"
#include "CalCulate.hpp"
using namespace protocol_ns;


class PackageParse
{
public:
    static void Parse(Connection *conn)
    {

        std::string package;
        Request req;
        Calculate cal;
        while (true)
        {
            // sleep(1);

            // 2.分析数据确定完整的报文
            package = Decode(conn->Inbuffer());
            if (package.empty()) //?????????????????????????????????????????????????????????
                break;           //?????????????????????????????????????????????????????????
            std::cout << "------------------------begin---------------" << std::endl;
            std::cout << "resq string:\n"
                      << package << std::endl;

            // 走到这里一定读到一个完整的json串！
            // 3.反序列化
            req.Deserialize(package);

            // 4.业务处理
            Response resp = cal.Excute(req);

            // 5.对应答序列化
            std::string send_str;
            resp.Serialize(&send_str);

            std::cout << "resq Serialize:" << std::endl;
            std::cout << send_str << std::endl;

            // 6.添加长度报头
            send_str = Encode(send_str);
            std::cout << "resq Encode:" << std::endl;
            std::cout << send_str << std::endl;

            conn->AppendOutBuffer(send_str);
        }

        if (!conn->OutbufferEmpty() && conn->_sender != nullptr)
        {
            conn->_sender(conn);

            //conn->_R->EnableReadWrite(conn->Sockfd(), true, true);

        }
    }
};
